package com.agile.major.web.rest;

import com.agile.major.domain.FreshGraduateEmployment;
import com.agile.major.service.FreshGraduateEmploymentService;
import com.agile.major.web.rest.util.HeaderUtil;
import com.agile.major.web.rest.util.PaginationUtil;
import com.codahale.metrics.annotation.Timed;
import io.github.jhipster.web.util.ResponseUtil;
import io.swagger.annotations.ApiParam;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.net.URI;
import java.net.URISyntaxException;
import java.util.List;
import java.util.Map;
import java.util.Optional;

/**
 * REST controller for managing FreshGraduateEmployment.
 */
@RestController
@RequestMapping("/api")
public class FreshGraduateEmploymentResource {

    private final Logger log = LoggerFactory.getLogger(FreshGraduateEmploymentResource.class);

    private static final String ENTITY_NAME = "freshGraduateEmployment";

    private final FreshGraduateEmploymentService freshGraduateEmploymentService;

    public FreshGraduateEmploymentResource(FreshGraduateEmploymentService freshGraduateEmploymentService) {
        this.freshGraduateEmploymentService = freshGraduateEmploymentService;
    }

    /**
     * POST  /fresh-graduate-employments : Create a new freshGraduateEmployment.
     *
     * @param freshGraduateEmployment the freshGraduateEmployment to create
     * @return the ResponseEntity with status 201 (Created) and with body the new freshGraduateEmployment, or with status 400 (Bad Request) if the freshGraduateEmployment has already an ID
     * @throws URISyntaxException if the Location URI syntax is incorrect
     */
    @PostMapping("/fresh-graduate-employments")
    @Timed
    public ResponseEntity<FreshGraduateEmployment> createFreshGraduateEmployment(@RequestBody FreshGraduateEmployment freshGraduateEmployment) throws URISyntaxException {
        log.debug("REST request to save FreshGraduateEmployment : {}", freshGraduateEmployment);
        if (freshGraduateEmployment.getId() != null) {
            return ResponseEntity.badRequest().headers(HeaderUtil.createFailureAlert(ENTITY_NAME, "idexists", "A new freshGraduateEmployment cannot already have an ID")).body(null);
        }
        FreshGraduateEmployment result = freshGraduateEmploymentService.save(freshGraduateEmployment);
        return ResponseEntity.created(new URI("/api/fresh-graduate-employments/" + result.getId()))
            .headers(HeaderUtil.createEntityCreationAlert(ENTITY_NAME, result.getId().toString()))
            .body(result);
    }

    /**
     * PUT  /fresh-graduate-employments : Updates an existing freshGraduateEmployment.
     *
     * @param freshGraduateEmployment the freshGraduateEmployment to update
     * @return the ResponseEntity with status 200 (OK) and with body the updated freshGraduateEmployment,
     * or with status 400 (Bad Request) if the freshGraduateEmployment is not valid,
     * or with status 500 (Internal Server Error) if the freshGraduateEmployment couldn't be updated
     * @throws URISyntaxException if the Location URI syntax is incorrect
     */
    @PutMapping("/fresh-graduate-employments")
    @Timed
    public ResponseEntity<FreshGraduateEmployment> updateFreshGraduateEmployment(@RequestBody FreshGraduateEmployment freshGraduateEmployment) throws URISyntaxException {
        log.debug("REST request to update FreshGraduateEmployment : {}", freshGraduateEmployment);
        if (freshGraduateEmployment.getId() == null) {
            return createFreshGraduateEmployment(freshGraduateEmployment);
        }
        FreshGraduateEmployment result = freshGraduateEmploymentService.save(freshGraduateEmployment);
        return ResponseEntity.ok()
            .headers(HeaderUtil.createEntityUpdateAlert(ENTITY_NAME, freshGraduateEmployment.getId().toString()))
            .body(result);
    }

    /**
     * GET  /fresh-graduate-employments : get all the freshGraduateEmployments.
     *
     * @param pageable the pagination information
     * @return the ResponseEntity with status 200 (OK) and the list of freshGraduateEmployments in body
     */
    @GetMapping("/fresh-graduate-employments")
    @Timed
    public ResponseEntity<List<FreshGraduateEmployment>> getAllFreshGraduateEmployments(@ApiParam Pageable pageable) {
        log.debug("REST request to get a page of FreshGraduateEmployments");
        Page<FreshGraduateEmployment> page = freshGraduateEmploymentService.findAll(pageable);
        HttpHeaders headers = PaginationUtil.generatePaginationHttpHeaders(page, "/api/fresh-graduate-employments");
        return new ResponseEntity<>(page.getContent(), headers, HttpStatus.OK);
    }

    /**
     * GET  /fresh-graduate-employments/:id : get the "id" freshGraduateEmployment.
     *
     * @param id the id of the freshGraduateEmployment to retrieve
     * @return the ResponseEntity with status 200 (OK) and with body the freshGraduateEmployment, or with status 404 (Not Found)
     */
    @GetMapping("/fresh-graduate-employments/{id}")
    @Timed
    public ResponseEntity<FreshGraduateEmployment> getFreshGraduateEmployment(@PathVariable Long id) {
        log.debug("REST request to get FreshGraduateEmployment : {}", id);
        FreshGraduateEmployment freshGraduateEmployment = freshGraduateEmploymentService.findOne(id);
        return ResponseUtil.wrapOrNotFound(Optional.ofNullable(freshGraduateEmployment));
    }

    /**
     * DELETE  /fresh-graduate-employments/:id : delete the "id" freshGraduateEmployment.
     *
     * @param id the id of the freshGraduateEmployment to delete
     * @return the ResponseEntity with status 200 (OK)
     */
    @DeleteMapping("/fresh-graduate-employments/{id}")
    @Timed
    public ResponseEntity<Void> deleteFreshGraduateEmployment(@PathVariable Long id) {
        log.debug("REST request to delete FreshGraduateEmployment : {}", id);
        freshGraduateEmploymentService.delete(id);
        return ResponseEntity.ok().headers(HeaderUtil.createEntityDeletionAlert(ENTITY_NAME, id.toString())).build();
    }
    @GetMapping("/fresh-graduate-employments/{baseMajorId}/{year}")
    @Timed
    public ResponseEntity<List<FreshGraduateEmployment>> getFreshGraduateEmploymentMap(@PathVariable Long baseMajorId,@PathVariable String year) {
        List<FreshGraduateEmployment> result = freshGraduateEmploymentService.getFreshGraduateList(baseMajorId,year);

        return ResponseUtil.wrapOrNotFound(Optional.ofNullable(result));
    }

    @GetMapping("/fresh-graduate-employments/stu-total/{baseMajorId}/{year}")
    @Timed
    public ResponseEntity getGraduateInfo(@PathVariable Long baseMajorId,@PathVariable String year) {
        Map result = freshGraduateEmploymentService.getGraduateStuCount(baseMajorId,year);
        return new ResponseEntity(result, HttpStatus.OK);
    }
}
